package com.sunnada.edu.business.attendance.service;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sunnada.edu.business.attendance.dao.PieceQueryDao;
import com.sunnada.edu.system.pub.common.EXCELObject;
import com.sunnada.edu.system.pub.util.DateUtil;

@Service
@Transactional
public class PieceQueryServiceImpl implements PieceQueryService {
	private static final Logger logger = LoggerFactory.getLogger(PieceQueryServiceImpl.class);
	@Resource
	PieceQueryDao pieceQueryDao;

	@Override
	public int getPieceQueryCots(HashMap<String, String> params) {
		return pieceQueryDao.getPieceQueryCots(params);
	}

	@Override
	public List<?> findPieceQuery(HashMap<String, String> params) {
		return pieceQueryDao.findPieceQuery(params);
	}

	@Override
	public String attendanceDelete(HashMap<String, String> params) {
		String msg = "";
		int num = pieceQueryDao.attendanceDelete(params);
		if (num == 0) {
			msg = "删除失败!";
		}
		return msg;
	}

	@Override
	public String attenRecordAdd(HashMap<String, String> params) {
		String msg = "";
			int num = pieceQueryDao.attenRecordAdd(params);
			if (num == 0) {
				msg = "0";// 失败
			} else {
				msg = "1";// 成功
			}

		return msg;
	}

	@SuppressWarnings("unchecked")
	@Override
	public String attendanceUpdate(HashMap<String, String> params) {
		String msg = "";
			int num = pieceQueryDao.attendanceUpdate(params);
			if (num == 0) {
				msg = "0";// 失败
			} else {
				msg = "1";// 成功
			}
		return msg;
	}

	@Override
	public Map attendanceByRecId(HashMap<String, String> params) {
		return pieceQueryDao.attendanceByRecId(params);
	}
	@Override
	public void exportPiece(HashMap<String, String> params, HttpServletRequest request, HttpServletResponse response) throws Exception {
		List dataList = pieceQueryDao.exportPiece(params);
		String recId = UUID.randomUUID().toString();
		String realPath = request.getSession().getServletContext().getRealPath("/file/temp");
		EXCELObject s = new EXCELObject();
		s.seteFilePath(realPath);
		String[] titH = { "姓名", "工号", "所属班组","工种","工程", "开始时间", "结束时间","累积数量","状态" };
		String[] titN = { "NAME","JOBNO","GROUPNAME","WORKTYPE","PROJECT_NAME","BEGIN_DATE","END_DATE","PIECE_NUM","AUDIT_RESULTS"};
		s.setFname("计件数据 "); // 临时文件名
		s.setTitle("计件数据"); // 大标题名称
		s.setTitH(titH);
		s.setTitN(titN);
		s.setDataList(dataList);
		File exportFile = null;
		exportFile = s.setData();
		String excelName = "计件数据" + DateUtil.getCurrentTime() + ".xls";
		s.exportExcel("计件数据", excelName, exportFile, request, response);

	}

}
